//
//  LSViewController.m
//  LibraryStorm
//
//  Created by Enel@Lightning on 3/21/14.
//  Copyright (c) 2014 3ATeam. All rights reserved.
//

#import "LSViewController.h"
#import "UILabel+NavigationTitleView.h"
#import "UIColor+HexString.h"
#import "UIImage+ColorExtension.h"
#import <iAd/iAd.h>

@interface LSViewController () <ADBannerViewDelegate>
{
    UILabel *lblTitle;
}
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightLayoutBannerView;

@end

@implementation LSViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

#pragma mark - UIViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    // Support iOS 7
    if (SD_IS_IOS7) {
        self.edgesForExtendedLayout = UIRectEdgeLeft | UIRectEdgeBottom | UIRectEdgeRight;
        self.extendedLayoutIncludesOpaqueBars = YES;
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    // Create custom title view
    lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.navigationController.navigationBar.frame.size.width, self.navigationController.navigationBar.frame.size.height)];
    /*
    self.navigationItem.titleView = lblTitle;
     */
    [lblTitle setNavigationTitleView:@""];

    if (SD_IS_IOS7) {
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithHexString:Color_Orange_View] withSize:CGSizeMake(1, 1)] forBarMetrics:UIBarMetricsDefault];
        [self setNeedsStatusBarAppearanceUpdate];
    }
    else {
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithHexString:Color_Orange_View] withSize:CGSizeMake(320, 44)] forBarMetrics:UIBarMetricsDefault];
    }

    [self.view setBackgroundColor:[UIColor whiteColor]];
}

#pragma mark - Memory Manager
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self layoutAnimated:NO];
    if ([self respondsToSelector:@selector(setCanDisplayBannerAds:)]) {
        self.canDisplayBannerAds = NO;
    }
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    isPushToOtherVC = NO;
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
}

- (void)dealloc
{

}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

#pragma mark - Set Title
/*
- (void)setTitleViewWithString:(NSString*)title {
    [lblTitle setNavigationTitleView:title];
}
 */

#pragma mark - Banner iAd
/**
 * Method name: layoutAnimated
 * Description: Show or hide banner iAd.
 * Parameters: BOOL
 */
- (void)layoutAnimated:(BOOL)animated
{
    CGFloat constant = 0.0f;

    // all we need to do is ask the banner for a size that fits into the layout area we are using
    if (_bannerView.bannerLoaded) {
        CGSize sizeForBanner = [_bannerView sizeThatFits:self.view.bounds.size];
        constant = sizeForBanner.height;
    } else {
        // hide the banner off screen further off the bottom
        constant = 0.0f;
    }

    _heightLayoutBannerView.constant = constant;
}

- (void)viewDidLayoutSubviews
{
    [self layoutAnimated:[UIView areAnimationsEnabled]];
    [self.view layoutSubviews];
}

- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
    [self layoutAnimated:YES];
}

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
//    NSLog(@"didFailToReceiveAdWithError %@", error);
    [self layoutAnimated:YES];
}

- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
{
    return YES;
}

- (void)bannerViewActionDidFinish:(ADBannerView *)banner
{
    
}

@end
